package com.gxc.test;

import java.io.IOException;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

/**
 * 测试 spring-aware.
 *
 * @author 宫新程
 * @since 2018/12/17 12:09 AM
 */
@Service
public class AwareService implements BeanNameAware, ResourceLoaderAware {

  private String beanName;
  private ResourceLoader resourceLoader;

  @Override
  public void setBeanName(String beanName) {
    this.beanName = beanName;
  }

  @Override
  public void setResourceLoader(ResourceLoader resourceLoader) {
    this.resourceLoader = resourceLoader;
  }

  public void outPutResource() {
    System.out.println("beanName：" + beanName);
    String path = ResourceLoader.CLASSPATH_URL_PREFIX.concat("test/hello.txt");
    System.out.println(path);
    Resource resource = resourceLoader.getResource(path);
    try {
      String str = IOUtils.toString(resource.getInputStream());
      System.out.println(str);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
